package com.example.forumsystem.services.impl;

import com.example.forumsystem.common.AppResult;
import com.example.forumsystem.common.ResultCode;
import com.example.forumsystem.dao.BoardMapper;
import com.example.forumsystem.exception.ApplicationException;
import com.example.forumsystem.model.Board;
import com.example.forumsystem.services.IBoardService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class BoardServiceImpi implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    /**
     * 根据 num 获取板块
     *
     * @param num 值
     * @return
     */

    @Override
    public List<Board> selectByNum(Integer num) {
        // 校验参数合法性
        if (num == null || num == 0) {

            // 打印日志
            log.info("参数不合法,num:" + num);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 根据值获获取板信息
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //校验参数
        if (id == null || id <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString() + " boardId = " + id);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 根据id获取板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 校验板块信息
        if (board == null || board.getId() <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        // 创建修改的对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        // 校验通过, 修改板块文章数量
        log.info("开始修改板块信息");
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        log.info("修改板块信息成功");

        // 校验是否修改成功
        if (row != 1) {
            // 打印日志
            log.info(ResultCode.FAILED_UPDATE.toString() + ", row:" + row);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_UPDATE));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //校验参数
        if (id == null || id <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString() + " boardId = " + id);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 根据id获取板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 校验板块信息
        if (board == null || board.getId() <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        // 创建修改的对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 如果文章数量小于0, 就设置为0
        if(updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }

        // 校验通过, 修改板块文章数量
        log.info("开始修改板块信息");
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        log.info("修改板块信息成功");

        // 校验是否修改成功
        if (row != 1) {
            // 打印日志
            log.info(ResultCode.FAILED_UPDATE.toString() + ", row:" + row);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_UPDATE));
        }
    }

    @Override
    public Board selectById(Long id) {
        //校验参数
        if (id == null || id <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString() + " boardId = " + id);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getId() <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + " boardId = " + id);
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }

}
